/**
 * 
 */
package de.cbf.cam.dsa.model.entities;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import de.cbf.cam.dsa.model.entities.states.OneState;
import de.cbf.cam.dsa.model.persistence.Header;

/**
 * @author Nebelritter
 *
 */
@Entity
public class StateManager extends Header {
	
	
	/**
	 * @uml.property  name="states"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.states.OneState"
	 */
	private List<OneState> states = new ArrayList<OneState>();
	
	public static String PROPERTYNAME= "StateManager:";
	/**
	 * @uml.property  name="propertyChangeSupport"
	 */
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	/**
	 * @uml.property  name="propertyChangeAdvance"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.model.entities.StateManager$PropertyChangeAdvanceListener"
	 */
	private PropertyChangeAdvanceListener propertyChangeAdvance = new PropertyChangeAdvanceListener();
	
	/**
	 * 
	 */
	public StateManager() {
		
	}

	public void addState(OneState state){
		states.add(state);
		state.propertyChangeSupport.addPropertyChangeListener(propertyChangeAdvance);
	}
	
	public void removeState(OneState state){
		states.remove(state);
		state.propertyChangeSupport.removePropertyChangeListener(propertyChangeAdvance);
	}
	@Transient
	public String getStatesDescription() {
		StringBuffer buffer = new StringBuffer();
		for(int i =0;i<states.size();i++){
			buffer.append(states.get(i).getDescription());
			if(i!=states.size()-1){
				buffer.append(",");
			}
		}
		return buffer.toString();
	}
	@Transient
	public List<OneState> getAllStatesOfType(int stateType){
		List<OneState> result = new ArrayList<OneState>();
		for(int i =0;i<states.size();i++){
			if(states.get(i).getStateType()==stateType){
				result.add(states.get(i));
			}
		}
		return result;
	}
	
	public void removeAllStatesOfType(int stateType){
		for(int i =0;i<states.size();i++){
			if(states.get(i).getStateType()==stateType){
				states.remove(states.get(i));
				i--;
			}
		}
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"specials", null, states);
	}
	private class PropertyChangeAdvanceListener implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+evt.getPropertyName(),null,StateManager.this);
		}		
	}
	/**
	 * @return the specials
	 */
	@ManyToMany(cascade=CascadeType.ALL)
	protected List<OneState> getStates() {
		return states;
	}

	/**
	 * @param specials the specials to set
	 */
	protected void setStates(List<OneState> states) {
		this.states = states;
	}

	@Override
	@Id
	public String getGuid() {
		return guid;
	}

	@Override
	protected void setGuid(String guid) {
		this.guid = guid;		
	}
}
